package detect.parse.tree;

import java.util.*;

import detect.parse.tree.token.PositionString;

public class FunctionCallManager {
	static HashMap<String,List<FunctionCallExpression>> returnVarMap=new HashMap<String,List<FunctionCallExpression>>();
	static HashMap<String,HashMap<FunctionCallExpression,Integer>> argMap=new HashMap<String,HashMap<FunctionCallExpression,Integer>>();
	static List<FunctionCallExpression> emptyFunCallList=new ArrayList<FunctionCallExpression>();
	static HashMap<FunctionCallExpression,Integer> emptyFunCallMap=new HashMap<FunctionCallExpression,Integer>();
	
	
	public static Set<Map.Entry<String, List<FunctionCallExpression>>> returnVarEntrySet(){
		return returnVarMap.entrySet();
	}	
	
	public static Set<Map.Entry<String, HashMap<FunctionCallExpression,Integer>>> argMapEntrySet(){
		return argMap.entrySet();
	}
	
	public static void putFunction(Expression exp){
		if(exp.getType()!=TreeType.FunctionCallType){
			return;
		}
		FunctionCallExpression funCallExp=(FunctionCallExpression)exp;
		PositionString returnVar=funCallExp.returnVar;
		List<Expression> argExpList=funCallExp.argExpList;
		
		if(returnVar!=null && returnVar.str!=null && returnVar.str.length()>0){
			putReturnVarFunction(returnVar.str,funCallExp);
		}
		
		for(int i=0;i<argExpList.size();i++){
			putArgFunction(argExpList.get(i).toString(),funCallExp,i+1);
		}
	}
	public static void putReturnVarFunction(String returnVar,FunctionCallExpression funCallExp){
		List<FunctionCallExpression> list=returnVarMap.get(returnVar);
		if(list==null){
			list=new ArrayList<FunctionCallExpression>(2);			
		}
		list.add(funCallExp);
	}
	
	public static void putArgFunction(String arg,FunctionCallExpression funCallExp,int argPos){//argPos=1,2,3...
		HashMap<FunctionCallExpression,Integer> map=argMap.get(arg);
		if(map==null){
			map=new HashMap<FunctionCallExpression,Integer>();
		}
		map.put(funCallExp,argPos);
	}
	
	public static List<FunctionCallExpression> getReturnVarFunctionList(String returnVar){
		List<FunctionCallExpression> list=returnVarMap.get(returnVar);
		if(list!=null){
			return list; 
		}
		return emptyFunCallList;
	}
	
	public static HashMap<FunctionCallExpression,Integer> getArgFunctionMap(String arg){
		HashMap<FunctionCallExpression,Integer> map=argMap.get(arg);
		if(map!=null){
			return map; 
		}
		return emptyFunCallMap;
	}
}
